#  (C) 1993 by Argonne National Laboratory and Mississipi State University.
#      All rights reserved.  See COPYRIGHT in top-level directory.
#

##### User configurable options #####

top_srcdir           = @top_srcdir@

CC                   = @CC@
CC_SHL               = @CC_SHL@
CLINKER              = @CLINKER@
MPI_CC               = @MPI_CC@
MPI_CLINKER          = $(MPI_CC)
AR                   = @AR@
ARFLAGS              = cr
RANLIB               = @RANLIB@
MAKE                 = @MAKE@
MPI_CFLAGS           = @MPI_CFLAGS@
MPI_LIBS             = @MPI_LIBS@
INCLUDE_DIR          = -I.. -I${top_srcdir}/include \
                       -I../../.. -I${top_srcdir}/../../include
PROF_LIBNAME         = @PROF_LIBNAME@
CFLAGS               = @CFLAGS@
MPE_LIBDIR           = @libbuild_dir@
LIB_PATH             = $(MPI_LIBS)
LDFLAGS              = @LDFLAGS@
MPI_IMPL             = @MPI_IMPL@
MPE_BUILD_FORTRAN2C  = @MPE_BUILD_FORTRAN2C@
MPEDBG               = @MPEDBG@
CC_DOES_C_O          = @CC_DOES_C_O@

### End User configurable options ###

SHELL            = @SHELL@
RM               = @RM@
MV               = @MV@
CP               = @CPRP@

@VPATH@

# Installation directories
prefix           = @prefix@
exec_prefix      = @exec_prefix@
bindir           = @bindir@
includedir       = @includedir@
libdir           = @libdir@
# Build directories
libbuild_dir     = @libbuild_dir@
binbuild_dir     = @binbuild_dir@
srcdir           = @srcdir@

EXECS            = dbxtest@EXEEXT@ dbxtest2@EXEEXT@

# GETNAME_DEFS are needed by mpehname.c
CC_FLAGS         = $(INCLUDE_DIR) $(CFLAGS) $(MPE_COPTS)
CC_LDFLAGS       = $(LDFLAGS) $(CFLAGS) $(MPE_LDOPTS)
# MPE include path, x_INCS, goes in front of MPI include path, MPI_xFLAGS,
# in case MPI include path is in MPI_xFLAGS containing old MPE include path.
MPICC_FLAGS      = $(INCLUDE_DIR) $(MPI_CFLAGS) $(MPE_COPTS)
MPICC_LDFLAGS    = $(LDFLAGS) $(MPI_CFLAGS) $(MPE_LDOPTS)

MPE_CSOURCES     = decomp.c mpe_seq.c dbxerr.c getgrank.c \
                   examine.c privtags.c mpehname.c mpe_io.c
# MPE_TSOURCES     = decomp.c mpe_seq.c dbxerr.c getgrank.c
MPE_P_COBJECTS   = $(MPE_CSOURCES:.c=.p@OBJEXT@)
# MPE_N_COBJECTS   = $(MPE_CSOURCES:.c=.n@OBJEXT@)

MPE_WSOURCES     = decompf.c mpe_seqf.c getgrankf.c
MPE_P_WOBJECTS   = $(MPE_WSOURCES:.c=.p@OBJEXT@)
# MPE_N_WOBJECTS   = $(MPE_WSOURCES:.c=.n@OBJEXT@)

MPE_XSOURCES     = mpehname.c
# MPE_P_XOBJECTS   = $(MPE_XSOURCES:.c=.p@OBJEXT@)
MPE_N_XOBJECTS   = $(MPE_XSOURCES:.c=.n@OBJEXT@)

MPE_SHLCOBJECTS  = $(MPE_COBJECTS:.@OBJEXT@=.lo)
MPE_SHLWOBJECTS  = $(MPE_WOBJECTS:.@OBJEXT@=.lo)

.SUFFIXES: .c .@OBJEXT@ .p@OBJEXT@ .n@OBJEXT@ .lo .a @EXEEXT@

.c.p@OBJEXT@:
	@if [ "$(CC_DOES_C_O)" = "yes" ]; then \
	     cmd="$(MPI_CC) $(MPICC_FLAGS) -o $@ -c $<" ; \
	 else \
	     cmd="$(MPI_CC) $(MPICC_FLAGS) -c $< ; $(MV) $*.@OBJEXT@ $*.p@OBJEXT@" ; \
	 fi ; \
	 echo "$${cmd}" ; $${cmd}

.c.n@OBJEXT@:
	@if [ "$(CC_DOES_C_O)" = "yes" ]; then \
	     cmd="$(CC) $(CC_FLAGS) -DMPE_NOMPI -o $@ -c $<" ; \
	 else \
	     cmd="$(CC) $(CC_FLAGS) -DMPE_NOMPI -c $< ; $(MV) $*.@OBJEXT@ $*.n@OBJEXT@" ; \
	 fi ; \
	 echo "$${cmd}" ; $${cmd}

# Allow compilers that do not allow -o for renaming object files
.c.lo:
	$(CC_SHL) $(CC_FLAGS) -c $< $(INC_DIR)
	@$(MV) $*.@OBJEXT@ $*.lo

# default_all is the same as default, but without the RANLIB.  This
# can speed up the build (ranlibs can take a LONG time).  profile_all 
# is the same, but for the profile library
# **** Currently the same as default for simplicity ****
default_all: default
profile_all: 

#
default:
	$(MAKE) $(MAKEOPTS) mpe_debug_objs 
	$(MAKE) $(MAKEOPTS) ${libbuild_dir}/lib$(PROF_LIBNAME)_nompi.a
	$(MAKE) $(MAKEOPTS) ${libbuild_dir}/lib$(PROF_LIBNAME).a

#
# This target will get compiled if the user specified --with-mpedbg option
mpe_debug_objs:
	@-if [ "$(MPEDBG)" = "yes" -a "$(MPI_IMPL)" = "MPICH" ] ; then \
	    echo "Making MPICH's mpedbg" ;  \
	    ( cd ../../../../src/env ; $(MAKE) $(MAKEOPTS) mpe_debug_objs ) ; \
	fi

#
# It is tempting here to use ... libmpe.a($(MPE_OBJECTS)) as the dependency,
# but this would require that all makes properly handle library dependencies.
# Since makes that DO are the exception rather than the rule, we don't
# use this form
#
# We've also had trouble with this.  Sometimes, under Solaris, the 
# first file in the dependency is built but not included with $? 
# This is clearly a bug in the make, since the determination of dependencies
# is done once (e.g., a blip in the filesystem time can't be 
# responsible if make works as Sun documents it).  To fix this, we
# add all the objects to the library, not just the "recent" ones.
${libbuild_dir}/lib$(PROF_LIBNAME).a: $(MPE_P_COBJECTS)
	$(AR) $(ARFLAGS) $@ $(MPE_P_COBJECTS)
	@if [ "$(MPE_BUILD_FORTRAN2C)" = "yes" ] ; then \
	     $(MAKE) $(MAKEOPTS) ${MPE_P_WOBJECTS} ; \
	     $(AR) $(ARFLAGS) $@ ${MPE_P_WOBJECTS} ; \
	 fi
	$(RANLIB) $@

${libbuild_dir}/lib$(PROF_LIBNAME)_nompi.a: $(MPE_N_XOBJECTS)
	 $(AR) $(ARFLAGS) $@ $(MPE_N_XOBJECTS)
	 $(RANLIB) $@

dbxtest@EXEEXT@: dbxtest.p@OBJEXT@ ${libbuild_dir}/lib$(PROF_LIBNAME).a
	$(CP) -f dbxtest.p@OBJEXT@ mpe2tmp.@OBJEXT@
	$(MPI_CLINKER) $(MPICC_LDFLAGS) -o $@ mpe2tmp.@OBJEXT@ \
	               -L${libbuild_dir} -l$(PROF_LIBNAME) $(MPI_LIBS) 
	@-$(RM) -f mpe2tmp.@OBJEXT@

dbxtest2@EXEEXT@: dbxtest2.p@OBJEXT@ 
	$(CP) -f dbxtest2.p@OBJEXT@ mpe2tmp.@OBJEXT@
	$(MPI_CLINKER) $(MPICC_LDFLAGS) -o $@ mpe2tmp.@OBJEXT@ \
	               $(LIB_PATH) 
	@-$(RM) -f mpe2tmp.@OBJEXT@

#
# Remove the old libraries before rebuilding (in case they came from another
# architecture or are otherwise damaged)
rmlib:
	@-$(RM) -f ${libbuild_dir}/lib$(PROF_LIBNAME).a 

rmprog:
	@-$(RM) -f $(EXECS)

clean: rmprog
	@-$(RM) -f *.*@OBJEXT@ ${srcdir}/*.*@OBJEXT@ *~ PI*

distclean: clean rmlib
	@-$(RM) -f Makefile dbxerr.c
